package com.module.product.manager;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.Session;

import base.dao.GenericHibernateDao;
import base.db.HibernateSessionFactory;
import base.manager.GenericHibernateManager;
import base.util.Page;
import base.util.StringUtil;

import com.module.product.common.ProductConstant;
import com.module.product.dao.AttrItemComboxDao;
import com.module.product.dao.AttrItemDao;
import com.module.product.dao.AttrValueDao;
import com.module.product.dao.AttrValueTemporaryDao;
import com.module.product.dao.CategoryProductDao;
import com.module.product.dao.EntAttrValueDao;
import com.module.product.dao.EntProductDao;
import com.module.product.dao.ProductDao;
import com.module.product.dao.ProductTemporaryDao;
import com.module.product.vo.AttrItem;
import com.module.product.vo.Category;
import com.module.product.vo.EntProduct;
import com.module.product.vo.ProductTemporary;
import com.util.PropertiesUtil;

/**
* 商品业务处理类
*
* @author  王勇云
* @version  [V1.0, Jul 22, 2011]
* @see  [相关类/方法]
* @since  [二代服务平台/商品]
*/
public class ProductTemporaryManager extends GenericHibernateManager<EntProduct,String>{
    private static final ProductTemporaryManager instance = new ProductTemporaryManager();
    
    private String downloadPath = PropertiesUtil.getString("downloadPath");
	
	private String downloadHostName = PropertiesUtil.getString("downloadHostName");
	
	private int downloadPort = Integer.parseInt(PropertiesUtil.getString("downloadPort"));
    
    static Logger logger = Logger.getLogger(ProductTemporaryManager.class.getName());
    private ProductTemporaryManager(){}
    
    public synchronized static ProductTemporaryManager getInstance() {
        return instance;
    }
    EntProductDao dao = EntProductDao.getInstance();
	AttrItemComboxDao comboxDao = AttrItemComboxDao.getInstance();
    ProductTemporaryDao productTemporaryDao = ProductTemporaryDao.getInstance();
    AttrValueDao attrValueDao = AttrValueDao.getInstance();
    AttrItemDao attrItemDao = AttrItemDao.getInstance(); 
    CategoryProductDao categoryProductDao = CategoryProductDao.getInstance();
    
    ProductDao productDao = ProductDao.getInstance();   
    AttrValueTemporaryDao attrValueTemporaryDao = AttrValueTemporaryDao.getInstance();
    EntAttrValueDao entAttrValueDao = EntAttrValueDao.getInstance();
    
    
    
    //审核商品列表
    public Page getList(ProductTemporary obj,Page page) throws Exception{
    	Session session =HibernateSessionFactory.getSession();
    	Connection conn = session.connection();
    	Page p = null;
    	try{
    		 p = productTemporaryDao.getList(session,obj,page);
    	        setCategoryName(conn,p);
    	}catch(Exception e){
    		throw e;
    		
    	}finally{
    		this.close(conn);
    		this.closeSession();
    	}

        return p;
    }
    
    
   
    
    private void setCategoryName(Connection conn ,Page page) throws SQLException{
        List list = page.getRows();
        Category c = null;
        if(list!=null){
            for(ProductTemporary p :(List<ProductTemporary>)list){
                p.setCategoryName(getCategoryNamesFromList(categoryProductDao.findCategoryListbyProductId(conn,p.getId())));
            }
        }
    }
    
    private String getCategoryNamesFromList(List<Category> list){
        StringBuffer categoryNames = new StringBuffer();
        if(list!=null){
            for(int i = 0;i<list.size();i++){
                if(i!=0){
                    categoryNames.append(" , "); 
                }
                categoryNames.append(list.get(i).getName());
            }
        } 
        return categoryNames.toString();
    }

    public ProductTemporary findById(String id) throws Exception
    {
    	Session session =HibernateSessionFactory.getSession();
    	Connection conn = session.connection();
    	ProductTemporary p = null;
    	try{
    		p = productTemporaryDao.get(session,id);
			p.setCategoryList(categoryProductDao.findCategoryIdbyProductId(conn,id));
    	}catch(Exception e){
    		throw e;
    		
    	}finally{
    		this.close(conn);
    		this.closeSession();
    	}

        return p;
    }
    public boolean update(ProductTemporary productTemporary) throws Exception{
    	Session session =HibernateSessionFactory.getSession();
    	boolean flag = false;
    	try{
    	    	flag = productTemporaryDao.update(session,productTemporary);
	    	}catch(Exception e){
	    		throw e;
	    		
	    	}finally{
	    		this.closeSession();
	    	}
    	return flag;
    }
    public List<AttrItem> listByCategoryId(AttrItem obj) throws Exception{
    	Session session =HibernateSessionFactory.getSession();
    	Connection conn = session.connection();
    	List<AttrItem> list  = new ArrayList();
    	try{
    		List<AttrItem> atrtlist  = attrValueTemporaryDao.listByCategoryId(conn,obj.getAttrGroup(),obj.getProductId());
    		list = this.getComboxVaule(atrtlist, session);
    	}catch(Exception e){
    		throw e;
    		
    	}finally{
    		this.close(conn);
    		this.closeSession();
    	}
        return list;
    }
    
    private List<AttrItem> getComboxVaule(List<AttrItem> list,Session session) throws Exception{
		List<AttrItem> attrList  = new ArrayList<AttrItem>();	
		if(list != null && ! list.isEmpty()){
			for(AttrItem atrrItem : list){
				List<String> comboxList = comboxDao.getComboxByAtrrId(session, atrrItem.getId());
				atrrItem.setComboxList(comboxList);
				attrList.add(atrrItem);	
				if(this.isImgOrVideo(atrrItem)){
					if(!StringUtil.isNull(atrrItem.getAttrValue())){
						atrrItem.setAttrValue(ProductConstant.HTTP_MEET+downloadHostName+":"+downloadPort+downloadPath + atrrItem.getAttrValue());
					}				
				}
			}
		}		
		return  attrList;
	}
   
    /**
	 * 是否是图片或者视频
	 * @param item
	 * @return
	 */
	private boolean isImgOrVideo(AttrItem item){
		if(item != null && item.getType() != null
				&&(item.getType().equals(ProductConstant.ATTR_TYPE_PICTURE) 
		    			|| item.getType().equals(ProductConstant.ATTR_TYPE_VEDIO)))
			return true;
		return false;
	}
	
	

	@Override
	public GenericHibernateDao<EntProduct, String> getDao() {
		// TODO Auto-generated method stub
		return dao;
	}
    
}
